package day018.homework.p4;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TicketTest implements Runnable{
    private int ticketNum=100;
    private final Lock lock=new ReentrantLock();

    @Override
    public void run() {
        while(true){
            lock.lock();
            try{
                if(ticketNum<=0){
                    break;
                }else{
                    ticketNum--;
                    try{
                        Thread.sleep(100);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+" 售出了一张票，还剩："+ticketNum+"张票");
                }
            }finally {
                lock.unlock();
            }
        }
    }
}
